﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_JobsiteCommandDtls : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IJobSiteBLL jobBLL = new JobSiteBLL();
        private ICommandBLL cmdBLL = new CommandBLL();
        private IJobsiteCommandDtlsBLL dtlsBLL = new JobsiteCommandDtlsBLL();

        private CommandInfo cmdInfo;
        private JobSiteCommandDtlsInfo dtlsInfo;

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadCmbOldJobSiteSeq();
            LoadCmbNewJobSiteSeq();

            lblCmdSeq.Text = Request.QueryString["CmdSeq"].ToString();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    //cmbJobSite.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "JOB_SITE_NAME").ToString(), DataBinder.Eval(DataItem, "JOB_SITE_SEQ").ToString()));
                    cmbJobSite.SelectedValue = DataBinder.Eval(DataItem, "JOB_SITE_SEQ").ToString();

                    //cmbOldJobSiteSeq.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FROM_UPPER_NAME").ToString(), DataBinder.Eval(DataItem, "FROM_UPPER_SEQ").ToString()));
                    cmbOldJobSiteSeq.SelectedValue = DataBinder.Eval(DataItem, "FROM_UPPER_SEQ").ToString();

                    //cmbNewJobSiteSeq.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "TO_UPPER_NAME").ToString(), DataBinder.Eval(DataItem, "TO_UPPER_SEQ").ToString()));
                    cmbNewJobSiteSeq.SelectedValue = DataBinder.Eval(DataItem, "TO_UPPER_SEQ").ToString();
                    txtOldCmdDate.SetValue(DataBinder.Eval(DataItem, "OLD_CMD_DATE", "{0:dd/MM/yyyy}").ToString());
                    txtOldStartDate.SetValue(DataBinder.Eval(DataItem, "OLD_START_DATE", "{0:dd/MM/yyyy}").ToString());
                    chkCancel.Checked = DataBinder.Eval(DataItem, "CANCEL").ToString() == "1";
                    CheckCancel(chkCancel.Checked ? true : false);
                    txtDateStart.SetValue(DataBinder.Eval(DataItem, "DATE_START", "{0:dd/MM/yyyy}").ToString());
                }
                else
                {

                }

            }

        }

        protected void LoadCmbOldJobSiteSeq()
        {
            if (cmbOldJobSiteSeq.Items.Count == 0)
            {
                cmbOldJobSiteSeq.DataSource = jobBLL.LoadJobSiteSearchText(GetJobSiteType(), "", "");
                cmbOldJobSiteSeq.DataBind();

                cmbOldJobSiteSeq.Items.Insert(0, new RadComboBoxItem("< เลือกข้อมูลหน่วยงานต้นสังกัด >", ""));
                cmbOldJobSiteSeq.SelectedIndex = 0;
            }

        }
        protected void LoadCmbNewJobSiteSeq()
        {
            if (cmbNewJobSiteSeq.Items.Count == 0)
            {
                cmbNewJobSiteSeq.DataSource = jobBLL.LoadJobSiteSearchText(GetJobSiteType(), "", "");
                cmbNewJobSiteSeq.DataBind();

                cmbNewJobSiteSeq.Items.Insert(0, new RadComboBoxItem("< เลือกข้อมูลหน่วยงานต้นสังกัด >", ""));
                cmbNewJobSiteSeq.SelectedIndex = 0;
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected string GetJobSiteType()
        {
            string JobSiteType = "";
            Nullable<int> Sequence = lblCmdSeq.Text.Length > 0 ? Int32.Parse(lblCmdSeq.Text) : (int?)null;
            IList<CommandInfo> cmdList = cmdBLL.ListCommand(SystemInfo._CmdJobSite, Sequence, (int?)null, (int?)null);
            if (cmdList.Count > 0)
            {
                cmdInfo = cmdList[0];

                if (cmdInfo.CmdFlag == "2")
                    JobSiteType = "2";
                else
                    JobSiteType = "1";
            }
            return JobSiteType;
        }
        protected void cmbJobSite_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbJobSite.Items.Clear();

            DataSet ds = jobBLL.LoadJobSiteSearchText(GetJobSiteType(), "", e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbJobSite.Items.Add(new RadComboBoxItem(dt.Rows[i]["JOB_SITE_NAME"].ToString(), dt.Rows[i]["JOB_SITE_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdFieldName = new TableCell();
                Label lblFieldName = new Label();
                lblFieldName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdFieldName.Width = Unit.Pixel(300);
                tdFieldName.Controls.Add(lblFieldName);
                tr.Cells.Add(tdFieldName);

                ((Table)cmbJobSite.Items[j].FindControl("tableJobSite")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }
        //protected void cmbOldJobSiteSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        //{
        //    cmbOldJobSiteSeq.Items.Clear();

        //    DataSet ds = jobBLL.LoadJobSiteSearchText(GetJobSiteType(), "", e.Text);
        //    DataTable dt = ds.Tables[0];

        //    int itemOffset = e.NumberOfItems;
        //    int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
        //    e.EndOfItems = (endOffset == dt.Rows.Count);
        //    int j = 0;
        //    for (int i = itemOffset; i < endOffset; i++)
        //    {
        //        cmbOldJobSiteSeq.Items.Add(new RadComboBoxItem(dt.Rows[i]["JOB_SITE_NAME"].ToString(), dt.Rows[i]["JOB_SITE_SEQ"].ToString()));

        //        TableRow tr = new TableRow();
        //        TableCell tdFieldName = new TableCell();
        //        Label lblFieldName = new Label();
        //        lblFieldName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
        //        tdFieldName.Width = Unit.Pixel(300);
        //        tdFieldName.Controls.Add(lblFieldName);
        //        tr.Cells.Add(tdFieldName);

        //        ((Table)cmbOldJobSiteSeq.Items[j].FindControl("tableOldJobSite")).Controls.Add(tr);
        //        j++;
        //    }
        //    e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        //}
        //protected void cmbNewJobSiteSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        //{
        //    cmbNewJobSiteSeq.Items.Clear();

        //    DataSet ds = jobBLL.LoadJobSiteSearchText(GetJobSiteType(), "", e.Text);
        //    DataTable dt = ds.Tables[0];

        //    int itemOffset = e.NumberOfItems;
        //    int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
        //    e.EndOfItems = (endOffset == dt.Rows.Count);
        //    int j = 0;
        //    for (int i = itemOffset; i < endOffset; i++)
        //    {
        //        cmbNewJobSiteSeq.Items.Add(new RadComboBoxItem(dt.Rows[i]["JOB_SITE_NAME"].ToString(), dt.Rows[i]["JOB_SITE_SEQ"].ToString()));

        //        TableRow tr = new TableRow();
        //        TableCell tdFieldName = new TableCell();
        //        Label lblFieldName = new Label();
        //        lblFieldName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
        //        tdFieldName.Width = Unit.Pixel(300);
        //        tdFieldName.Controls.Add(lblFieldName);
        //        tr.Cells.Add(tdFieldName);

        //        ((Table)cmbNewJobSiteSeq.Items[j].FindControl("tableNewJobSite")).Controls.Add(tr);
        //        j++;
        //    }
        //    e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        //}
        protected void CheckDateValid(object source, ServerValidateEventArgs args)
        {
            //if (!DateTimeX.IsValidDmyDate(args.Value, true))
            //{
            //    args.IsValid = false;
            //}
        }

        protected void cmbJobSite_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //IList<JobSiteInfo> jobInfo = jobBLL.ListJobSiteBySequence(cmbJobSite.SelectedValue);
            DataSet ds = jobBLL.LoadJobSites(cmbJobSite.SelectedValue, "", "", "");
            DataTable dt = null;
            
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    txtOldRefNo.Text = dt.Rows[0]["JOB_SITE_REF"].ToString();
                    txtNewRefNo.Text = dt.Rows[0]["JOB_SITE_REF"].ToString();
                    txtOldName.Text = dt.Rows[0]["JOB_SITE_NAME"].ToString();
                    txtNewName.Text = dt.Rows[0]["JOB_SITE_NAME"].ToString();
                    cmbOldJobSiteSeq.SelectedValue = dt.Rows[0]["upper_job_site_seq"].ToString();
                    cmbNewJobSiteSeq.SelectedValue = dt.Rows[0]["upper_job_site_seq"].ToString();
                }
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            dtlsInfo = new JobSiteCommandDtlsInfo();

            dtlsInfo.JobSiteCommandSeq = lblCmdSeq.Text.Length > 0 ? Int32.Parse(lblCmdSeq.Text) : (int?)null;
            dtlsInfo.JobSiteSeq = cmbJobSite.SelectedValue.Length > 0 ? Int32.Parse(cmbJobSite.SelectedValue) : (int?)null;
            dtlsInfo.OldRefNo = txtOldRefNo.Text.Length > 0 ? txtOldRefNo.Text : "";
            dtlsInfo.NewRefNo = txtNewRefNo.Text.Length > 0 ? txtNewRefNo.Text : "";
            dtlsInfo.OldName = txtOldName.Text.Length > 0 ? txtOldName.Text : "";
            dtlsInfo.NewName = txtNewName.Text.Length > 0 ? txtNewName.Text : "" ;
            dtlsInfo.OldUpperSeq = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldJobSiteSeq.SelectedValue) : (int?)null;
            dtlsInfo.NewUpperSeq = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewJobSiteSeq.SelectedValue) : (int?)null;
            dtlsInfo.OldUpperName = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? cmbOldJobSiteSeq.Text : "";
            dtlsInfo.NewUpperName = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? cmbNewJobSiteSeq.Text : "";

            dtlsInfo.OldCmdRefNo = txtOldCmdRefNo.Text;
            dtlsInfo.OldCmdDate = txtOldCmdDate.GetStringValue().Length > 0 ? DateTime.Parse(txtOldCmdDate.GetStringValue()) : (DateTime?)null;
            dtlsInfo.OldStartDate = txtOldStartDate.GetStringValue().Length > 0 ? DateTime.Parse(txtOldStartDate.GetStringValue()) : (DateTime?)null;

            dtlsInfo.Cancel = (chkCancel.Checked ? "1" : "0");
            dtlsInfo.StartDate = txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null;
            dtlsInfo.CreatedBy = ssMng.EmpSeqI;

            dtlsBLL.Insert(dtlsInfo);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            dtlsInfo = new JobSiteCommandDtlsInfo();

            dtlsInfo.Seq = lblSeq.Text.Length > 0 ? Int32.Parse(lblSeq.Text) : (int?)null;
            dtlsInfo.JobSiteCommandSeq = lblCmdSeq.Text.Length > 0 ? Int32.Parse(lblCmdSeq.Text) : (int?)null;
            dtlsInfo.JobSiteSeq = cmbJobSite.SelectedValue.Length > 0 ? Int32.Parse(cmbJobSite.SelectedValue) : (int?)null;
            dtlsInfo.OldRefNo = txtOldRefNo.Text.Length > 0 ? txtOldRefNo.Text : "";
            dtlsInfo.NewRefNo = txtNewRefNo.Text.Length > 0 ? txtNewRefNo.Text : "";
            dtlsInfo.OldName = txtOldName.Text.Length > 0 ? txtOldName.Text : "";
            dtlsInfo.NewName = txtNewName.Text.Length > 0 ? txtNewName.Text : "";
            dtlsInfo.OldUpperSeq = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbOldJobSiteSeq.SelectedValue) : (int?)null;
            dtlsInfo.NewUpperSeq = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? Int32.Parse(cmbNewJobSiteSeq.SelectedValue) : (int?)null;
            dtlsInfo.OldUpperName = cmbOldJobSiteSeq.SelectedValue.Length > 0 ? cmbOldJobSiteSeq.Text : "";
            dtlsInfo.NewUpperName = cmbNewJobSiteSeq.SelectedValue.Length > 0 ? cmbNewJobSiteSeq.Text : "";
            dtlsInfo.OldCmdRefNo = txtOldCmdRefNo.Text;
            dtlsInfo.OldCmdDate = txtOldCmdDate.GetStringValue().Length > 0 ? DateTime.Parse(txtOldCmdDate.GetStringValue()) : (DateTime?)null;
            dtlsInfo.OldStartDate = txtOldStartDate.GetStringValue().Length > 0 ? DateTime.Parse(txtOldStartDate.GetStringValue()) : (DateTime?)null;

            dtlsInfo.Cancel = (chkCancel.Checked ? "1" : "0");
            dtlsInfo.StartDate = txtDateStart.GetStringValue().Length > 0 ? DateTime.Parse(txtDateStart.GetStringValue()) : (DateTime?)null;
            dtlsInfo.UpdatedBy = ssMng.EmpSeqI;

            dtlsBLL.Update(dtlsInfo);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
        protected void CheckCancel(bool bCheck)
        {
            if (bCheck)
            {
                txtOldRefNo.Text = "";
                txtNewRefNo.Text = "";
                txtOldName.Text = "";
                txtNewName.Text = "";
                cmbOldJobSiteSeq.SelectedValue = null;
                cmbNewJobSiteSeq.SelectedValue = null;
            }
        }
        protected void chkCancel_CheckedChanged(object sender, EventArgs e)
        {
            CheckCancel(chkCancel.Checked ? true : false);
        }

        protected void cmbOldJobSiteSeq_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }
    }
}